package com.study.webapp.leetcode;

/**
 * 会先判断下instance是否为null,对象一旦被创建，后期调用就不会进入synchronized同步，效率较高
 */
public class Singleton2 {

    private volatile static Singleton2 instance;
    private Singleton2() {
    }

    public static Singleton2 getInstance() {
        if (instance == null) {
            synchronized (Singleton2.class) {
                if (instance == null) {
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
}
